#define NUMBER_EDIT_MAIN
#include "Number_Edit.h"
#include "..\func_key32\key.h"

//返回=0，按键没用掉，=1按键被用掉了
unsigned char Number_Edit(NUMBER_EDIT_T *p,unsigned char key_code)
{
	float f;
	long bit_add;
	int i;
	int r;
	union{
		unsigned char uc;
		char c;
		unsigned short us;
		short s;
		unsigned long ul;
		long l;
	}u;
		
	if(p->b_edit)
	{
		r=1;
		
		bit_add=1;						//根据当前编辑位计算加减的值
		for(i=0;i<p->cur_bit;i++)
		{
			bit_add*=10;
		}
		
		switch(key_code)
		{
			case KEY_CODE_UP:			//加
				p->val += bit_add;
				if(p->val > p->v.max)
				{
					p->val=p->v.max;
				}
				break;
			case KEY_CODE_DOWN:			//减
				p->val -= bit_add;
				if(p->val < p->v.min)
				{
					p->val=p->v.min;
				}
				break;
			case KEY_CODE_LEFT:			//左移
				p->cur_bit++;
				if(p->cur_bit >= p->max_bit)
				{
					p->cur_bit=0;
				}
				break;
			case KEY_CODE_RIGHT:		//右移
				p->cur_bit--;
				if(p->cur_bit & 0x80)
				{
					p->cur_bit=p->max_bit-1;
				}
				break;
			case KEY_CODE_OK:			//确认
				u.l=p->val;
				switch(p->v.val_type)
				{
					case VAL_TYPE_BIT:
					case VAL_TYPE_U8:
						*(unsigned char *)p->v.adr=u.uc;
						break;
					case VAL_TYPE_S8:
						*(char *)p->v.adr=u.c;
						break;
					case VAL_TYPE_U16:
						*(unsigned short *)p->v.adr=u.us;
						break;
					case VAL_TYPE_S16:
						*(short *)p->v.adr=u.s;
						break;
					case VAL_TYPE_U32:
						*(unsigned long *)p->v.adr=u.ul;
						break;
					case VAL_TYPE_S32:
						*(long *)p->v.adr=u.l;
						break;
					case VAL_TYPE_FLOAT:
						f=u.l;
						for(i=0;i<p->v.dec_nb;i++)
						{
							f/=10;
						}
						*(float *)p->v.adr=f;
						break;
					
				}
				p->b_edit=0;
				break;
			case KEY_CODE_CANCEL:		//取消
				p->b_edit=0;
				break;
			default:
				r=0;					//不是编辑键
				break;
		}
	}
	else
	{
		r=0;							//非编辑状态
	}
	
	
	if(r)
	{			//有有效按键按下，使正在编辑位保持反白
		left_ms_set(&p->tm_flash,NUMBER_EDIT_FLASH_DIS_MS);
		p->b_flash=1;
	}
	else if(left_ms(&p->tm_flash)==0)
	{
		left_ms_set(&p->tm_flash,NUMBER_EDIT_FLASH_DIS_MS);
		p->b_flash=!p->b_flash;
	}

	if(r)
	{
		return(0);
	}
	else
	{
		return(key_code);
	}
}

//开始进入对一个变量的编辑
void Number_Edit_Init(NUMBER_EDIT_T *p,VAL_PROPERTY_T *v)
{				
	union{
		unsigned long ul;
		long l;
	}u;
	int i,j;
	float f;
	//--------------------------------------
	//求设定最大位数
	j=1;				//先求正向
	for(i=0;i<10;i++)
	{
		if(j > v->max)
		{
			break;
		}
		j*=10;
	}
	p->max_bit=i;
	
	j=-1;				//再求负向
	for(i=0;i<10;i++)
	{
		if(j < v->min)
		{
			break;
		}
		j*=10;
	}
	if(i>p->max_bit)	//取大值
	{
		p->max_bit=i;
	}
	p->cur_bit=0;
	//--------------------------------------
	p->v=*v;					//属性指针
	p->b_edit=1;			//正在编辑状态
	switch(v->val_type)		//把变量值赋给编辑临时变量
	{
		case VAL_TYPE_BIT:
		case VAL_TYPE_U8:
			u.ul=*(unsigned char *)v->adr;
			break;
		case VAL_TYPE_S8:
			u.l=*(char *)v->adr;
			break;
		case VAL_TYPE_U16:
			u.ul=*(unsigned short *)v->adr;
			break;
		case VAL_TYPE_S16:
			u.l=*(short *)v->adr;
			break;
		case VAL_TYPE_U32:
			u.ul=*(unsigned long *)v->adr;
			break;
		case VAL_TYPE_S32:
			u.l=*(long *)v->adr;
			break;
		case VAL_TYPE_FLOAT:
			f=*(float *)v->adr;
			for(i=0;i<p->v.dec_nb;i++)
			{
				f*=10;
			}
			u.l=f;
			break;
	}
	p->val=u.l;				//放入结构体
	
}
